package com.fashiontextile.admin.service.impl;

import com.fashiontextile.admin.entity.Dict;
import com.fashiontextile.admin.entity.DictCategory;
import com.fashiontextile.admin.mapper.DictMapper;
import com.fashiontextile.admin.service.DictService;
import com.fashiontextile.common.entity.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class DictServiceImpl implements DictService {
    DictMapper dictMapper;

    @Override
    public Dict selectDictByDid(Long did) {
        Dict dict = dictMapper.selectDictByDid(did);
        if (dict.getParentId() != null) {
            dict.setParent(selectDictByDid(dict.getParentId()));
        }
        return dict;
    }

    @Override
    public List<Dict> selectDictChildrenByDids(List<Long> dids) {
        return dictMapper.selectDictChildrensByDids(dids);
    }

    @Override
    public List<Dict> selectDictByCategoryId(Long dcid) {
        List<Dict> dicts = dictMapper.selectDictByCategoryId(dcid, null);

        // 储存父字典id
        List<Long> ids = new ArrayList<>();
        for (Dict dict : dicts) {
            ids.add(dict.getDid());
        }

        if (ids.size() > 0) {
            // 查询所有子字典
            List<Dict> childDicts = selectDictChildrenByDids(ids);

            // 将子字典添加到父字典中
            for (Dict dict : dicts) {
                List<Dict> children = new ArrayList<>();
                for (Dict child : childDicts) {
                    if (dict.getDid().equals(child.getParentId())) {
                        children.add(child);
                    }
                }
                dict.setChildren(children);
            }
        }

        return dicts;
    }

    @Override
    public List<DictCategory> selectAllDictCategory() {
        return dictMapper.selectAllDictCategory();
    }

    @Override
    public DictCategory selectDictCategoryBydicd(Long dicd) {
        return dictMapper.selectDictCategory(dicd);
    }

    @Override
    public Dict selectDictbyTypeAndValueAndParent(Long dcid, String value, Long parent) {
        return dictMapper.selectDictbyTypeAndValueAndParent(dcid, value, parent);
    }

    @Override
    public Integer insertDict(Dict dict) {
        if (dict.getParentId() != null) {
            if (selectDictByDid(dict.getParentId()) != null) {
                return dictMapper.insertDict(dict.getValue(), dict.getCategory().getDcid(), dict.getParentId());
            }
            return 0;
        }
        return dictMapper.insertDict(dict.getValue(), dict.getCategory().getDcid(), dict.getParentId());
    }

    @Override
    public Integer deleteDictByDid(Long did) {
        return dictMapper.deleteDictByDid(did);
    }

    @Override
    public Integer deleteDictCategoryByDcid(Long dcid) {
        return dictMapper.deleteDictCategoryByDcid(dcid);
    }

    @Override
    public DictCategory selectDictcategoryByname(String categoryName) {
        return dictMapper.selectDictcategoryByname(categoryName);
    }

    @Override
    public Integer insertDictcategory(String dictCategory) {
        return dictMapper.insertDictcategory(dictCategory);
    }
}
